﻿using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MvcSample.ModelBinders
{
    public class RmbModelBinder
        : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }
            if (bindingContext.ModelType==typeof(string))
            {
                var name = bindingContext.ModelName;
                var valueResult = bindingContext.ValueProvider.GetValue(name);

                if (valueResult != ValueProviderResult.None)
                {
                    var val = valueResult.FirstValue;

                    bindingContext.Result = ModelBindingResult.Success($"{DateTime.Now.ToShortTimeString()}:{val}");
                }
            }
            return Task.CompletedTask;
        }
    }
}
